package uk.ac.bath.genetic.tree;
import uk.ac.bath.genetic.bool.*;
import uk.ac.bath.genetic.base.Operator;
import uk.ac.bath.genetic.component.FoodSupply;
import uk.ac.bath.genetic.base.Point;
import uk.ac.bath.genetic.base.Cell;
import uk.ac.bath.genetic.base.Space;
import uk.ac.bath.genetic.mover.Environment;
import uk.ac.bath.genetic.base.Value;

import uk.ac.bath.genetic.plant.PlantCell;

import  uk.ac.bath.util.*;
import java.util.*;
import uk.ac.bath.genetic.base.Pallete;
import uk.ac.bath.genetic.base.PalleteIF;

/*
 * Environment.java
 *
 * Created on January 10, 2001, 11:44 AM
 */


public class TreeEnvironment extends Environment {

    Vector <Operator>      ops;
    PalleteIF pallete;
    FoodSupply food;

    public TreeEnvironment(Space space,FoodSupply food) {
        super(space,null);
	this.food=food;

	tweaks.addAll(food.getTweaks());
	tweaks.addAll(PlantCell.tweaks);
	tweaks.addAll(LogicalMachine.tweaks);


        ops=new Vector<Operator>();
        ops.add(new And());
        ops.add(new Or());
        ops.add(new Not());
        ops.add(new Link());
// 	ops.add(new X());
// 	ops.add(new Mult());
        ops.add(new On());
        ops.add(new Off());
        ops.add(new Int());
        pallete=new Pallete(ops,new Value(0.0));

    }




    public double  eatFoodAt(Point loc,double amount) {
	return food.eatFoodAt(loc,amount);
    }

    public double  getFoodAt(Point loc) {
	return food.getFoodAt(loc);
    }



    public void replenish() {
	food.replenishFood();
    }

//     public double  eatFoodAt(Point loc,double amount) {
// 	return amount;
//     }

//     public double  getFoodAt(Point loc) {
// 	return 10;// food.getFoodValueAt(loc);
//     }

//     public void replenish() {
// 	//	food.replenishFood();
//     }


//     public double getLightAt(Point p) {
// 	Point2D p2=(Point2D)p;

// 	double dx=p2.getX()-xLight;
// 	double dy=p2.getY()-yLight;
// 	double d2 = (dx*dx + dy*dy)+1;
// 	return 4.0/d2;

//     }


//     public Cell  cellAt(Point p) {

// 	if (!space.contains(p)) return null;
// 	Iterator iter=population_.iterator();

// 	while(iter.hasNext()) {
// 	    Cell cell = (Cell)iter.next();
// 	    if (cell.getPoint().equals(p)) return cell;
// 	}
// 	return null;
//     }

 


     public Cell createRandomCell() {
	 Point p = food.randomPoint();
	 if (cellAt(p) != null ) return null;
	 int nOps = TreeCell.minOperatorsPerCell + 
 	    MyRandom.nextInt(TreeCell.maxOperatorsPerCell.intValue()-TreeCell.minOperatorsPerCell);
 	LogicalMachine m=new LogicalMachine(nOps,pallete);

	int cid=Cell.nextId();
	String tag="P:" +  cid;

 	return new TreeCell(m,p,
 			     TreeCell.initialFitness.doubleValue(),
 			     Cell.nextId(),0,0,tag);
     }

//     public double [][] getFoodArray() {
// 	return food.getArray();
//     }


    public FoodSupply getFoodSupply() {
	return food;
    }

    @Override
    public void mutateCell(Cell cell) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

  
    
}

